/**
 * Map Analysis Service
 * 数据分析处理服务，对后台返回的数据进行处理
 */

angular.module('NxtMap')
  .service('MapAnalysis', ['ConstService', MapAnalysis]);

function MapAnalysis(ConstService) {

  this.minlng = 120.04418;
  this.minlat = 29.96215;
  this.s = 0.005;

  this.setS = function (s){
    this.s = s;
  }
  
  //计算图形格子数
  this.compute = function (data, formatParam) {
    var timeMap = {};
    var mapS = {};  //方格点
    var mapH = {};  //六边形点
    var mapStation = {};  //派出所内数据
    var stations = ConstService.stations;
    for(var i = 0; i < stations.length; i++){
      mapStation[stations[i]] = {
        bumen: stations[i],
        value: 0,
        objects: []
      };
    }
    for(var i = 0; i < data.length; i++){
      var keyS = data[i].spos.x + '-' + data[i].spos.y;
      var keyH = data[i].hpos.x + '-' + data[i].hpos.y;
      if(formatParam){
        var date = new Date(data[i].BJSJ);
        var keyT = date.format(formatParam);
        keyS = keyS + '-' + keyT;
        keyH = keyH + '-' + keyT;
        timeMap[keyT] = 1;
      }
      if(stations.indexOf(data[i].bumen) > -1){
        if(keyT){
          if(!mapStation[data[i].bumen][keyT]){
            mapStation[data[i].bumen][keyT] = {};
            mapStation[data[i].bumen][keyT].value = 0;
          }
          mapStation[data[i].bumen][keyT].value ++;
          if(mapStation[data[i].bumen].value < mapStation[data[i].bumen][keyT].value){
            mapStation[data[i].bumen].value = mapStation[data[i].bumen][keyT].value;
          }
        }
        else {
          mapStation[data[i].bumen].value ++;
          mapStation[data[i].bumen].objects.push(data[i]);
        }
      }
      
      if(!mapS[keyS]){
        mapS[keyS] = {};
        mapS[keyS].value = 1;
        mapS[keyS].bumen = data[i].bumen;
        mapS[keyS].objects = [];
        mapS[keyS].objects.push(data[i]);
      }
      else {
        mapS[keyS].value ++;
        mapS[keyS].objects.push(data[i]);
      }
      if(!mapH[keyH]){
        mapH[keyH] = {};
        mapH[keyH].value = 1;
        mapH[keyH].bumen = data[i].bumen;
        mapH[keyH].objects = [];
        mapH[keyH].objects.push(data[i]);
      }
      else {
        mapH[keyH].value ++;
        mapH[keyH].objects.push(data[i]);
      }
    }

    var result = {
      s: this.s,
      h: this.s / 2,
      r: this.s * Math.sqrt(3) / 2,
      minClng: this.minlng - this.s * Math.sqrt(3) / 2,
      minClat: this.minlat - this.s / 2 - this.s,
      minlng: this.minlng,
      minlat: this.minlat,
      maxS: 4,
      maxH: 4,
      maxStation: 10,
      dataS: [],
      dataH: [],
      dataStation: [],
      timelineList: []
    };
    for(var index in mapS){
      var square = {
        lngNo: index.split("-")[0],
        latNo: index.split("-")[1],
        bumen: mapS[index].bumen,
        value: mapS[index].value,
        objects: mapS[index].objects
      };
      if(formatParam){
        square.timeline = index.split("-")[2];
      }
      if(result.maxS < square.value) {
        result.maxS = square.value;
      }
      result.dataS.push(square);
    }
    result.dataS.sort(function (a, b) {
      return b.value - a.value;
    });

    for(var index in mapH){
      var hexagon = {
        lngNo: index.split("-")[0],
        latNo: index.split("-")[1],
        bumen: mapH[index].bumen,
        value: mapH[index].value,
        objects: mapH[index].objects
      };
      if(formatParam){
        hexagon.timeline = index.split("-")[2]; 
      }
      if(result.maxH < hexagon.value) {
        result.maxH = hexagon.value;
      }
      result.dataH.push(hexagon);
    }
    result.dataH.sort(function (a, b) {
      return b.value - a.value;
    });

    for(var index in mapStation){
      var station = mapStation[index];
      if(result.maxStation < station.value) {
        result.maxStation = station.value;
      }
      result.dataStation.push(station);
    }

    for(var index in timeMap){
      if(index){
        result.timelineList.push(index);
      }
    }
    result.timelineList.sort(function (a, b) {
      return a - b;
    });
    return result;
  }

  //计算各类警情数目
  this.countType = function (data, qhour){
    var map = {};
    var mapTime = {};
    for(var i = 0; i < data.length; i++) {
      if(!map[data[i].BJLBMC]){
        map[data[i].BJLBMC] = {};
        map[data[i].BJLBMC].value = 1;
        map[data[i].BJLBMC].objects = [];
        map[data[i].BJLBMC].objects.push(data[i]);
      }
      else {
        map[data[i].BJLBMC].value ++;
        map[data[i].BJLBMC].objects.push(data[i]);
      }
      if(!mapTime[data[i].XS]){
        mapTime[data[i].XS] = 1;
      }
      else {
        mapTime[data[i].XS] ++;
      }
    }
    var result = {
      map: [],
      mapTime: [],
      legend: [],
      legendTime: [],
      value: [],
      valueTime: []
    }
    for(var index in map){
      result.legend.push(index);
      result.value.push(map[index].value);
      result.map.push({name: index, value: map[index].value, objects: map[index].objects});
    }
    result.map.sort(function (a, b){
      return b.value - a.value;
    });
    for(var index in mapTime){
      result.mapTime.push({name: index, value: mapTime[index]});
    }
    result.mapTime.sort(function (a, b){
      return a.name - b.name;
    });
    var tmp = qhour[0] - 0;
    var tmpk = qhour[0];
    for(var i = 0; i < result.mapTime.length; i++){
      while(tmpk != result.mapTime[i].name){
        result.legendTime.push(tmpk);
        result.valueTime.push(0);
        tmp ++;
        var tmpk = '0' + tmp;
        tmpk = tmpk.substring(tmpk.length - 2, tmpk.length);
      }
      result.legendTime.push(result.mapTime[i].name);
      result.valueTime.push(result.mapTime[i].value);
      tmp ++;
      var tmpk = '0' + tmp;
      tmpk = tmpk.substring(tmpk.length - 2, tmpk.length);
    }
    for(; tmp < qhour[1]; tmp++){
      var tmpk = '0' + tmp;
      tmpk = tmpk.substring(tmpk.length - 2, tmpk.length);
      result.legendTime.push(tmpk);
      result.valueTime.push(0);
    }
    return result;
  }

  //统计路口点数
  this.countCross = function (data) {
    var map = {};
    var road_map = {};
    for(var i = 0; i < data.length; i++){
      if(data[i].crossId) {
        if(!map[data[i].crossId]) {
          map[data[i].crossId] = {};
          map[data[i].crossId].value = 0;
          map[data[i].crossId].position = [data[i].crosslng, data[i].crosslat];
          map[data[i].crossId].objects = [];
        }
        map[data[i].crossId].value ++;
        map[data[i].crossId].objects.push(data[i]);
      }
      else {
        if(!road_map[data[i].roadId]) {
          road_map[data[i].roadId] = {};
          road_map[data[i].roadId].value = 0;
          road_map[data[i].roadId].objects = [];
        }
        road_map[data[i].roadId].value ++;
        road_map[data[i].roadId].objects.push(data[i]);
      }
    }
    var result = {
      max: 1,
      road_max: 1,
      data: [],
      road_data: []
    }
    for(var index in map){
      var item = {
        index: index,
        value: map[index].value,
        position: map[index].position,
        objects: map[index].objects
      }
      if(item.value > result.max && item.position){
        result.max = item.value;
      }
      result.data.push(item);
    }
    result.data.sort(function (a, b) {
      return b.value - a.value;
    });
    for(var index in road_map){
      var item = {
        index: index,
        value: road_map[index].value,
        objects: road_map[index].objects
      }
      if(item.value > result.road_max) {
        result.road_max = item.value;
      }
      result.road_data.push(item);
    }
    result.road_data.sort(function (a, b) {
      return b.value - a.value;
    });
    return result;
  }

}
